import {trace} from './config';
import {getPageRandom, genSignNonce} from './util.js'
import {sendTraceData} from './trace';

export default {
	data() {
		return {
			__prandom: '',
			__currentPage: '',
			__recordData: {},
			__loadTime: 0,
			__lastPrandom:'',
		}
	},
    created() {
		if (trace) {
			let pages = getCurrentPages();
			let route = pages[pages.length - 1].route || '';
			let options = pages[pages.length - 1].$page.options;
			// console.log("+++++++++++fffffffffffffffff--------------------------------------进来应该会自动埋点才对啊",route)
			let loadTime = new Date().getTime();
			let recordData = this.__recordData;
			this.__loadTime = loadTime;
			this.__currentPage = '/' + route;
			recordData = Object.assign({}, recordData, options || {});
			let prandom = getPageRandom([
				genSignNonce(),
				this.__currentPage,
				this.__loadTime
			]);
			this.__prandom = prandom;
			this.__recordData = recordData;
			sendTraceData({
				url: this.__currentPage,
				lid: prandom,
				act: 'start',
				time: loadTime,
				args: JSON.stringify(recordData),
			});
		}
    },
    mounted() {
		if (this.__prandom != '') {
			let endTime = new Date().getTime();
			sendTraceData({
			 	lid: this.__prandom,
			 	act: 'end',
			 	time: endTime,
			});
		}
    },
    activated() {
		if (trace) {
			let pages = getCurrentPages();
			let route = pages[pages.length - 1].route || '';
			let options = pages[pages.length - 1].$page.options;
			let loadTime = new Date().getTime();
			let recordData = this.__recordData;
			this.__loadTime = loadTime;
			this.__currentPage = '/' + route;
			recordData = Object.assign({}, recordData, options || {});
			let prandom = getPageRandom([
				genSignNonce(),
				this.__currentPage,
				this.__loadTime
			]);
			this.__prandom = prandom;
			this.__recordData = recordData;
			// sendTraceData({
			// 	url: this.__currentPage,
			// 	lid: prandom,
			// 	act: 'start',
			// 	time: loadTime,
			// 	args: JSON.stringify(recordData),
			// });
		}

    },
    unmounted() {
		// console.log("子组件会不会发生离开事件呢")
		if (this.__prandom != '') {
			let leaveTime = new Date().getTime();
			sendTraceData({
				lid: this.__prandom,
				act: 'leave',
				time: leaveTime,
			});
		}
    },
	// onTabItemTap(item) {

	// 	let prandom = getPageRandom([
	// 		genSignNonce(),
	// 		this.__currentPage,
	// 		this.__loadTime
	// 	]);
	// 	//由A界面到B界面
	// 	// if (this.__prandom != '') {
	// 	// 	let leaveTime = new Date().getTime();
	// 	// 	sendTraceData({
	// 	// 		lid: this.__prandom,
	// 	// 		act: 'leave',
	// 	// 		time: leaveTime,
	// 	// 	});
	// 	// }
	
	// 	console.log(item.index)
	// 	    console.log(item.pagePath)
	// 	    console.log(item.text)
	// },
    destroyed() {
		if (this.__prandom != '') {
			let leaveTime = new Date().getTime();
			sendTraceData({
				lid: this.__prandom,
				act: 'leave',
				time: leaveTime,
			});
		}
        console.log(4, '页面销毁');
    }
}